package com.example.exercise.excel.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.example.exercise.common.base.BaseResponse;
import com.example.exercise.excel.service.ExcelService;
import com.example.exercise.excel.vo.EasyExcelDemoVO;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

@Controller
class EasyExcelController {
    @Resource
    private ExcelService excelService;

    @RequestMapping(value = "output1",method = RequestMethod.GET)
    public void outputExcel(HttpServletResponse httpServletResponse) {
        String fileName = "导出用户信息明细"+ new SimpleDateFormat("yyyyMMdd").format(System.currentTimeMillis());
        String contentDisposition = null;
        try {
            contentDisposition = "attachment; filename=" + URLEncoder.encode(fileName, "UTF-8") + ".et";
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        httpServletResponse.setHeader("Content-Disposition", contentDisposition);
        httpServletResponse.setCharacterEncoding("UTF-8");
        int size = 10000;
        List<EasyExcelDemoVO> list = new ArrayList<>();
        for (int i = 0; i < size; i++) {
            EasyExcelDemoVO demo = new EasyExcelDemoVO();
            demo.setAddress("北京 ");
            demo.setAge(55);
            demo.setName("小明用户" + i);
            demo.setSex("男");
            demo.setIncome(BigDecimal.valueOf(i*1000.3564));
            demo.setDescription("妈咪妈咪哄");
            list.add(demo);
        }

        try {
            EasyExcel.write(httpServletResponse.getOutputStream(), EasyExcelDemoVO.class)
                    .excelType(ExcelTypeEnum.XLSX)
                    .sheet(0)
                    .doWrite(list);
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                if(httpServletResponse.getOutputStream() != null) {
                    httpServletResponse.getOutputStream().close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

    @RequestMapping(value = "uploadAllColumn",method = {RequestMethod.POST},produces = { MediaType.APPLICATION_JSON_VALUE})
    @ResponseBody
    public BaseResponse<List<EasyExcelDemoVO>> uploadExcelAllColumn(@RequestParam("file") MultipartFile multipartFile) {

        BaseResponse<List<EasyExcelDemoVO>> baseResponse = new BaseResponse<>();
        List<EasyExcelDemoVO> list = EasyExcel.read(multipartFile.getOriginalFilename(), EasyExcelDemoVO.class, new AnalysisEventListener<EasyExcelDemoVO>() {
            @Override
            public void invoke(EasyExcelDemoVO demo, AnalysisContext analysisContext) {

            }

            @Override
            public void doAfterAllAnalysed(AnalysisContext analysisContext) {

            }
        }).sheet().doReadSync();

        List<EasyExcelDemoVO> list1 = new ArrayList<>();
        for (EasyExcelDemoVO demo : list) {
            list1.add(demo);
            System.out.println(demo);
        }
        baseResponse.setCode(0);
        baseResponse.setData(list1);
        return baseResponse;
    }

}
